
/**
 *	鱼眼菜单
 *
 *	纯js、css实现
 *	@author 攻心小虫
 **/


var fisheye = (function(){
	var min=45;
	addListener = function(element,eventName,fn){
		if(element.addEventListener){
			element.addEventListener(eventName,fn,false);
		}else if(element.attachEvent){
			element.attachEvent("on"+eventName,fn);
		}else{
			element["on"+eventName] = fn;
		}
	};
	menu = function(config){
		this.setting = config;
		this.init.call(this,arguments);
	};
	menu.prototype = {
		init:function(){
			var box = document.createElement("div");
			var fxTimer = null,icos= [];
			document.body.appendChild(box);
			box.className = "fisheye";
			for(var i=0;i<this.setting.items.length;i++){
				var item = this.setting.items[i];
				var A = document.createElement("a");
				var img = document.createElement("img");
				var span = document.createElement("span");
				box.appendChild(A);
				A.id = item.id;
				A.appendChild(img);
				A.appendChild(span);
				A.title = item.name;
				img.src = item.img;
				span.innerHTML = item.name;
				icos.push(A);
				if(item.handler){
					addListener(A,"click",item.handler);
				}
			}
			doSize(box,min,icos);
			box.onmousemove=function(e){
				if(fxTimer){
					clearTimeout(fxTimer);
				}
				var e= e ||event;
				fxTimer = setTimeout(function(){doSize(box,min,icos,e.clientX,e.clientY);},3);
            };
            box.onmouseout = function(e){//还原
            	var e = window.event || ae;  
                var s = e.toElement || e.relatedTarget;     
                if(document.all){    
                    if( !this.contains(s) ){     
                    	clearTimeout(fxTimer);
		            	doSize(box,min,icos);
                    }     
                }else{     
                    var res= this.compareDocumentPosition(s) ;       
                    if( ! ( res == 20 || res == 0) ){        
		            	clearTimeout(fxTimer);
		            	doSize(box,min,icos);
                    }       
                }    
            	
            };
		}
	};
	/** 设置大小 **/
	doSize=function (box,min,icos,_x,_y){
		var x =0,z=0;
		if(_x){
           x=_x-box.offsetLeft;
           var c=0;
           z=220-((_y>(c-100))?100:(c-_y));
           if(_y<(c-180))_x=false;
		};
		var unit=180/Math.min(500,box.offsetWidth);
		var size=function(_x,_w){
              var A=Math.round(90-(x-_x-_w+10)*unit);
              A=Math.max(0,Math.min(180,A))/180*Math.PI;
              return Math.max(Math.round(Math.sin(A)*z),min);
		};
	      for (var i=0,left=0,prev=0;i<icos.length;++i ) {
	              var _1=icos[i];
	              _1.style.width=(_x?size.call(this,left,prev*0.6):min)+'px';
	              prev=_1.offsetWidth;
	              left+=prev;
	      };
      
	};
	return menu;
})();